package com.example.wendao.view.ui.fragment;


import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.fragment.app.Fragment;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.wendao.App;
import com.example.wendao.R;
import com.example.wendao.common.PageUtils;
import com.example.wendao.model.entity.User;
import com.example.wendao.view.adapter.AgreementForumListAdapter;
import com.example.wendao.viewmodel.ForumViewModel;
import com.example.wendao.viewmodel.UserViewModel;


/**
 * A simple {@link Fragment} subclass.
 */
public class CollectForumFragment extends Fragment{
        private AgreementForumListAdapter adapter = new AgreementForumListAdapter();
        private ForumViewModel forumViewModel = null;
        private UserViewModel userViewModel = null;
        private String[] cforumId;
        private int[] arr;
        private String SQL = "";
        private User user;
        private RecyclerView recyclerView;


public CollectForumFragment(){
        // Required empty public constructor
        }


@Override
public View onCreateView(LayoutInflater inflater,ViewGroup container,
        Bundle savedInstanceState){
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_collectforum, container, false);
        recyclerView=view.findViewById(R.id.rv_collectforum);
        forumViewModel = new ViewModelProvider(this).get(ForumViewModel.class);
        userViewModel = new ViewModelProvider(App.getInstance()).get(UserViewModel.class);
        //将user中的点赞id字段提取出来到arr数组中
        user = userViewModel.getCurrentUser();
        cforumId=user.getCforumId().split(",");
        for (int i = 0; i < cforumId.length; i++) {
                arr[i] = Integer.parseInt(cforumId[i]);
                //  forumViewModel.setId(arr[i]);
                // forumViewModel.getForumById().observe(this.getViewLifecycleOwner(), adapter::submitList);
        }
        //将arr数组中的id封装成一段sql语句
        if(arr.length > 0) {
                SQL += " '";
                for(int i=0; i<arr.length-1; i++) {
                        SQL = SQL + arr[i] + "','";
                }
                SQL += arr[arr.length-1] + "'";
        }
        forumViewModel.getForumsByForumIds(SQL).observe(this.getViewLifecycleOwner(), adapter::submitList);
        PageUtils.initRecyclerView(recyclerView, adapter, new LinearLayoutManager(this.getActivity()));
        return view;
        }
}
